//
//  DropdownMenuView.m
//  LoveHealth
//
//  Created by 陈永坤 on 14-9-24.
//  Copyright (c) 2014年 lanou. All rights reserved.
//

#import "DropdownMenuView.h"

#import "HealthFoodClass.h"



#import "FoodDetailClassCollectionCell.h"

@interface DropdownMenuView ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic,retain)NSMutableArray * tableViewDataArray;
@property (nonatomic,retain)NSMutableDictionary * collectionViewDataDic;
@property (nonatomic,retain)NSMutableArray * collectionViewDataArray;
@property (nonatomic,retain)UIView * blankView;
@end

@implementation DropdownMenuView

- (void)dealloc
{
    Block_release(_hideMenuBlock);
    self.blankView = nil;
    self.collectionViewDataDic = nil;
    self.tableViewDataArray = nil;
    self.tableView = nil;
    self.collectionView = nil;
    [super dealloc];
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        [self setupSubview];
        [self setupDataSource];
    }
    return self;
}


- (void)setupSubview
{
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.0];
    
    _blankView = [[UIView alloc]init];
    [self addSubview:_blankView];
    [self addTapGesture];
    [_blankView release];
    
    
    
    _tableView = [[UITableView alloc]initWithFrame:self.frame style:UITableViewStylePlain];
    
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self addSubview:_tableView];
    [_tableView release];
    //默认选择第一个cell
    NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [_tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
    
    
    //创建collectionView的布局 使用系统提供的网格样式
    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc]init];
    
     //设置滚动方向 水平 或者 垂直
     flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
     
     //item大小
     flowLayout.itemSize = CGSizeMake(60, 30);
     
     //最小列间距
     flowLayout.minimumInteritemSpacing = 2;
     
     //最小行间距
     flowLayout.minimumLineSpacing = 2;
     
     //设置section内边距
     flowLayout.sectionInset = UIEdgeInsetsMake(2, 2, 2, 2);
    
    
    
    _collectionView = [[UICollectionView alloc]initWithFrame:self.frame collectionViewLayout:flowLayout];
    _collectionView.backgroundColor = UIColorDDMenuSelected;
    [self addSubview:_collectionView];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    //注册集合视图中使用的cell类型及重用标示
    [_collectionView registerClass:[FoodDetailClassCollectionCell class] forCellWithReuseIdentifier:@"Cell"];
    
}

- (void)addTapGesture{
    UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapGestureAciton:)];
    [self.blankView addGestureRecognizer:tapGesture];
    [tapGesture release];
}

- (void)handleTapGestureAciton:(UITapGestureRecognizer *)gesture
{
    self.hideMenuBlock(nil);
}
- (void)setupDataSource
{
    self.tableViewDataArray = [NSMutableArray arrayWithCapacity:10];
    
    NSString * filePath = [[NSBundle mainBundle]pathForResource:@"HealthFoodClass.txt" ofType:nil];
    NSData * data = [NSData dataWithContentsOfFile:filePath];
    
    NSArray *  sourceArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    
    for (NSDictionary * dic in sourceArray) {
         HealthFoodClass * foodClass = [[HealthFoodClass alloc]init];
        [foodClass setValuesForKeysWithDictionary:dic];
        [self.tableViewDataArray addObject:foodClass];
        NSLog(@"%@,%@",foodClass.ID, foodClass.name);
        [foodClass release];
    }
    
   
    self.collectionViewDataDic = [NSMutableDictionary dictionaryWithCapacity:14];
    self.collectionViewDataArray = [NSMutableArray arrayWithCapacity:10];
    NSString * filePathDic = [[NSBundle mainBundle]pathForResource:@"HealthFoodDetailClass.txt" ofType:nil];
    NSData * dataDic = [NSData dataWithContentsOfFile:filePathDic];
    
    NSDictionary *  sourceDic = [NSJSONSerialization JSONObjectWithData:dataDic options:NSJSONReadingMutableContainers error:nil];
    //NSLog(@"%@",sourceDic);
    for (NSDictionary * dicClass in sourceDic) {
        
        NSMutableArray * classArray = [NSMutableArray arrayWithCapacity:10];
        for (NSDictionary *dicDetail  in sourceDic[dicClass]) {
            HealthFoodDetailClass * foodDetailClass = [[HealthFoodDetailClass alloc]init];
            [foodDetailClass setValuesForKeysWithDictionary:dicDetail];
            [classArray addObject:foodDetailClass];
            [foodDetailClass release];
        }
        [self.collectionViewDataDic setObject:classArray forKey:dicClass];
    }
    self.collectionViewDataArray = self.collectionViewDataDic[@"1"];
}



- (void)beginShowWithRect:(CGRect)rect;
{
    // self.tableView.frame = CGRectMake(0,50, 300, 0);
   
    if (rect.origin.y<0) {
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
    }
    
    [UIView animateWithDuration:0.5 animations:^{
        self.frame = rect;
        self.tableView.frame = CGRectMake(self.bounds.origin.x, self.bounds.origin.y, self.bounds.size.width/3, self.bounds.size.height/2);
        self.collectionView.frame = CGRectMake(self.tableView.frame.origin.x+self.tableView.frame.size.width, self.bounds.origin.y, self.bounds.size.width/3*2, self.bounds.size.height/2);
        if (rect.origin.y>0) {
            self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
        }
        self.blankView.frame =CGRectMake(self.bounds.origin.x,self.tableView.frame.size.height , self.bounds.size.width, rect.size.height-self.tableView.frame.size.height);
    } completion:^(BOOL finished) {
        
    }];
    

}



//tableView 代理



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tableViewDataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static  NSString * identifier = @"cell";
    
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
        cell.selectedBackgroundView = [[[UIView alloc]initWithFrame:cell.frame] autorelease];
        cell.selectedBackgroundView.backgroundColor = UIColorDDMenuSelected;
    }
    HealthFoodClass * foodClass = self.tableViewDataArray[indexPath.row];
    cell.textLabel.text = foodClass.name;
    
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    HealthFoodClass * foodClass = self.tableViewDataArray[indexPath.row];
    self.collectionViewDataArray = self.collectionViewDataDic[[foodClass.ID stringValue]];
    [self.collectionView reloadData];
}


- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    cell.backgroundColor = UIColorDDMenuTableViewCell;
    cell.frame = CGRectMake(-320, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height);
    [UIView animateWithDuration:0.3 animations:^{
        cell.frame = CGRectMake(0, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height);
    } completion:^(BOOL finished) {
        ;
    }];
}




//collectionView 代理

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
 
    return self.collectionViewDataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //static NSString * identifiercollection = @"identifiercollection";
    FoodDetailClassCollectionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    
    cell.selectedBackgroundView = [[[UIView alloc]initWithFrame:cell.frame] autorelease];
    cell.selectedBackgroundView.backgroundColor = [UIColor grayColor];
    
    HealthFoodDetailClass * foodDetailClass = self.collectionViewDataArray[indexPath.row];
    //cell.backgroundColor = [UIColor grayColor];
    cell.foodDetailClassNameLabel.text = foodDetailClass.name;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%@",indexPath);
    HealthFoodDetailClass * foodDetailClass = self.collectionViewDataArray[indexPath.row];
    self.hideMenuBlock(foodDetailClass);
    
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {
 // Drawing code
 }
 */
@end
